package com.neuedu.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/")
@Api(value = "用户相关接口",description = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录",notes = "登录接口描述信息",httpMethod = "POST")
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<UserInfo> login(String username, String password, HttpSession session){
        JSONData data = userService.login(username,password);
        //成功
        if(data.getCode()==0){
            session.setAttribute(Const.CURRENT_USERINFO,data.getData());
            //修改用户最后登录时间
            userService.updateUserLastLoginTime(username);
        }
        return data;
    }

    @ApiOperation(value = "注册",notes = "注解接口描述信息",httpMethod = "POST")
    @RequestMapping(value = "register",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> register(UserInfo userInfo){
        JSONData data = userService.register(userInfo);
        return data;
    }
    @ApiOperation(value = "检查用户名是否存在",notes = "检查用户名是否存在",httpMethod = "POST")
    @RequestMapping(value = "checkUserName",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> checkUserName(String username){
        return userService.checkUserName(username);
    }
    @ApiOperation(value = "检查邮箱是否存在",notes = "检查邮箱是否存在",httpMethod = "POST")
    @RequestMapping(value = "checkEmail",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> checkEmail(String email){
        return userService.checkEmail(email);
    }
    @ApiOperation(value = "退出登录",notes = "退出登录",httpMethod = "GET")
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    @ResponseBody
    public JSONData<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USERINFO);
        return JSONData.buildSuccess("退出成功");
    }

    @ApiOperation(value = "获取当前登录用户信息",notes = "获取当前登录用户信息",httpMethod = "POST")
    @RequestMapping(value = "getLoginUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<UserInfo> getLoginUserInfo(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo != null){
            return JSONData.buildSuccess(userInfo);
        }
        return JSONData.buildError("用户未登录，无法获取其信息");
    }
    @ApiOperation(value = "获取用户的提示问题",notes = "获取用户的提示问题",httpMethod = "POST")
    @RequestMapping(value = "getUserQuestion",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> getUserQuestion(String username){
        return userService.getUserQuestion(username);
    }
    @ApiOperation(value = "校验用户的答案",notes = "校验用户的答案",httpMethod = "POST")
    @RequestMapping(value = "checkUserAnswer",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> checkUserAnswer(String username,String answer){
        return userService.checkUserAnswer(username,answer);
    }
    @ApiOperation(value = "重置密码",notes = "重置密码",httpMethod = "POST")
    @RequestMapping(value = "resetPassword",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> resetPassword(String username,String newpassword){
        return userService.resetPassword(username,newpassword);
    }
    @ApiOperation(value = "通过用户ID获取用户信息",notes = "通过用户ID获取用户信息",httpMethod = "POST")
    @RequestMapping(value = "getUserInfoById",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<UserInfo> getUserInfoById(int id){
        return userService.getUserInfoById(id);
    }
    @ApiOperation(value = "获取所有用户信息",notes = "获取所有用户信息",httpMethod = "POST")
    @RequestMapping(value = "getAllUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<UserInfo>> getAllUserInfo(){
        return userService.getAllUserInfo();
    }
    @ApiOperation(value = "根据ID修改用户信息",notes = "根据ID修改用户信息",httpMethod = "POST")
    @RequestMapping(value = "updateUserInfoById",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> updateUserInfoById(UserInfo userInfo){
        return userService.updateUserInfoById(userInfo);
    }

}
